/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Nov 7, 2013 3:52:36 PM
 */
package com.googlecode.backtest.document;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.googlecode.backtest.rule.ta.TAParameters;

/**
 * @author lianyuy
 * 
 */
@XmlRootElement(name = "Item")
@XmlType(name = "ItemType", propOrder = { "name", "sourceids", "param", "list" })
public class Item implements IItem {
	@XmlElementWrapper(name = "ItemList")
	@XmlElement(name = "Item")
	private ArrayList<Item> list;
	@XmlAttribute(name = "GUID")
	protected String GUID;
	@XmlElement(name = "ClassName")
	private String name;
	@XmlElement
	private TAParameters param;
	@XmlElement
	private List<String> sourceids = new ArrayList<String>();

	public Item() {
		super();
		Long uuid = UUID.randomUUID().getMostSignificantBits();
		GUID = Long.toHexString(uuid);
	}

	public Item(String ID) {
		this();
		GUID = ID;
	}

	public Item(String name, ArrayList<Item> list, TAParameters param) {
		this();
		this.name = name;
		this.list = list;
		this.param = param;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getClassName()
	 */
	@Override
	public String getClassName() {
		return name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IItem#setclassName(java.lang.String)
	 */
	@Override
	public void setclassName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getitemList()
	 */
	@Override
	public ArrayList<Item> getItemList() {
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IItem#setitemLists(java.util.ArrayList)
	 */
	@Override
	public void setitemLists(ArrayList<Item> list) {
		this.list = list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getitemID()
	 */
	// @XmlAttribute(required = false)
	@Override
	public String getItemID() {
		return GUID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#putitemID(java.lang.String)
	 */
	// @XmlAttribute(required = false)
	@Override
	public void setitemID(String GUID) {
		this.GUID = GUID;
	}

	/**
	 * @return the param
	 */
	public TAParameters getParam() {
		return param;
	}

	/**
	 * @param param
	 *            the param to set
	 */
	public void setparam(TAParameters param) {
		this.param = param;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#addSourceID(java.lang.String)
	 */
	@Override
	public void addSourceID(String id) {
		sourceids.add(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#clearSourceIDS()
	 */
	@Override
	public void clearSourceIDS() {
		sourceids.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getSourceCount()
	 */
	@Override
	public int getSourceCount() {
		return sourceids.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getSourceID(int)
	 */
	@Override
	public String getSourceID(int index) {
		return sourceids.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getIterator()
	 */
	@Override
	public Iterator<Item> getIterator() {
		if (list == null) {
			return null;
		}
		return list.iterator();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getSourceIDIterator()
	 */
	@Override
	public Iterator<String> getSourceIDIterator() {
		return sourceids.iterator();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IItem#getSourceIDs()
	 */
	@Override
	public List<String> getSourceIDs() {
		return sourceids;
	}

}
